import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';

import '../entity/receipt_address_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/default_views.dart';
import '../views/dialog_manager.dart';

///
/// 选择收货地址
///
class MineAddressChoose extends StatefulWidget {
  const MineAddressChoose({Key? key}) : super(key: key);

  @override
  State<MineAddressChoose> createState() => _MineAddressChooseState();
}

class _MineAddressChooseState extends State<MineAddressChoose> {
  List<ReceiptAddressData> datas = [];

  @override
  void initState() {
    super.initState();
    getReceipts();
  }

  getReceipts() async {
    dynamic result = await Api.receiptList();
    ReceiptAddressEntity entity = ReceiptAddressEntity.fromJson(result);
    setState(() {
      datas.clear();
      datas.addAll(entity.data);
    });
  }


  Widget getListItem(int index) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10.w, horizontal: 25.w),
      decoration: BoxDecoration(
        color: (Config.colorWhite),
        borderRadius: BorderRadius.circular(10.w),
      ),
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 25.w),
      child: InkWell(
        onTap: () {
          Routes.router.pop(context, datas[index]);
        },
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    Text(
                      '收货人：${datas[index].username}',
                      style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                    ),
                    Opacity(
                      opacity: datas[index].isDefault == "1" ? 1 : 0,
                      child: Container(
                        decoration: BoxDecoration(
                          color: Config.colorMain,
                          borderRadius: BorderRadius.circular(20.w),
                        ),
                        padding: EdgeInsets.symmetric(vertical: 3.w, horizontal: 10.w),
                        margin: EdgeInsets.only(left: 14.w),
                        child: Text(
                          "默认",
                          style: TextStyle(fontSize: 16.sp, color: Config.colorWhite),
                        ),
                      ),
                    ),
                  ],
                ),
                Text(
                  datas[index].mobile,
                  style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                ),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 20.w),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                // Icon(
                //   Icons.location_on_outlined,
                //   size: 32.w,
                //   color: Config.colorText1,
                // ),
                Expanded(
                  child: Text(
                    "收货地址：${datas[index].provinceName}${datas[index].cityName}${datas[index].areaName}${datas[index].detail}",
                    style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                  ),
                ),
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 5.w),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg2,
      appBar: AppBar(
        title: const Text('选择收货地址'),
        centerTitle: true,
        actions: <Widget>[
          TextButton(
            child: Text(
              '管理',
              style: TextStyle(color: Config.colorText1, fontSize: 26.w),
            ),
            onPressed: () {
              Routes.navigateTo(context, Routes.mineAddressManager).then((value) {
                if (value != null) {
                  getReceipts();
                }
              });
            },
          ),
        ],
      ),
      body: datas.isNotEmpty
          ? SizedBox(
              width: double.infinity,
              height: double.infinity,
              child: ListView.builder(
                shrinkWrap: true,
                physics: const BouncingScrollPhysics(),
                itemCount: datas.length,
                padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 0.w),
                itemBuilder: (BuildContext context, int index) {
                  return getListItem(index);
                },
              ),
            )
          : showNullData(),
    );
  }
}
